JavaScript Ne(eeeeee)rds. Help gotten! Issue solved. All hail Crowmolly!

Kinja'd!!! "anonsagainstanonymous" (anonsagainstanonymous)
03/19/2014 at 12:00 • Filed to: None

Kinja'd!!!0 Kinja'd!!! 17
Kinja'd!!!

I'm terribly sorry about going so far off-topic, but I am ripping my hair out right now and I've got nowhere else to go.

A friend of mine boasted she'd made a JavaScript cash register so I decided to try and make one of my own. I got it working with some very basic, repetitive code. I wanted to challenge myself and streamline it today, but I'm running into problems with it. I have declared global variables cashNumber and costNumber . Both of which are set to 0.

var cost; var costNumber = 0; var cash; var cashNumber = 0; var cashBack; var costText = ("Cost of merchandise"); var cashText = ("Cash Paid");

function costPrompt() { dataPrompt(cost, costText, costNumber, costPrompt); }

function cashPrompt() { dataPrompt(cash, cashText, cashNumber, cashPrompt); }

function dataPrompt(x, y, z, a) { x = prompt (y); z = parseFloat(x); console.log (z); console.log (a); if (isNaN(z)) { alert ("That's not a number, cumface. Try again."); a(); } }

costPrompt();

cashPrompt();

console.log ("cashNumber " + cashNumber); console.log ("costNumber " + costNumber);

Can any one explain to me why the changes made to cashNumber and costNumber inside dataPrompt does not change outside the function, even though the variable is global?


DISCUSSION (17)


Kinja'd!!! Aaron James > anonsagainstanonymous
03/19/2014 at 12:07

Kinja'd!!!1

use hers if it works. or is this some new age courtship ritual?


Kinja'd!!! vorspringing > anonsagainstanonymous
03/19/2014 at 12:08

Kinja'd!!!0

First thing I see is a typo in your variable declaration - var casNumber instead of cashNumber.


Kinja'd!!! anonsagainstanonymous > vorspringing
03/19/2014 at 12:12

Kinja'd!!!0

Yes, sorry. Thank you. The code I copied in was an old bugged version. and I'm sorry about the terrible layout. Kinja doesn't handle well and refuses to let me use line breaks.


Kinja'd!!! Nibby > anonsagainstanonymous
03/19/2014 at 12:15

Kinja'd!!!0

Next time, link to a pastebin?


Kinja'd!!! Eric the RC guy > anonsagainstanonymous
03/19/2014 at 12:18

Kinja'd!!!0

Can you post the full code with HTML since I'm too lazy to recreate it?

Global variables are screwy in javascript so I recommend against using them, let your functions return values instead.


Kinja'd!!! vorspringing > anonsagainstanonymous
03/19/2014 at 12:26

Kinja'd!!!0

No worries. Looking at it, I don't see where you're making changes to either of those variables within the dataPrompt function itself, which means it's going to return 0 for both values.


Kinja'd!!! crowmolly > anonsagainstanonymous
03/19/2014 at 12:26

Kinja'd!!!0

var cost; var costNumber; var cash; var cashNumber; var cashBack; var costText = ("Cost of merchandise"); var cashText = ("Cash Paid");

function costPrompt() { costNumber = dataPrompt(cost, costText, costNumber, costPrompt); }

function cashPrompt() { cashNumber = dataPrompt(cash, cashText, cashNumber, cashPrompt); }

function dataPrompt(x, y, z, a) { x = prompt (y); z = parseFloat(x); console.log (z); console.log (a); if (isNaN(z)) { alert ("That's not a number, cumface. Try again."); a(); } return z; }

costPrompt();

cashPrompt();

alert("costNumber " + costNumber);

alert("cashNumber " + cashNumber);

Terrible formatting is terrible.

But you get the basic idea. Return the values.


Kinja'd!!! anonsagainstanonymous > Eric the RC guy
03/19/2014 at 12:27

Kinja'd!!!0

http://pastebin.com/JcqAzdy0 I've been trying to figure out how to use the return function. I've got it working so-so at this point.


Kinja'd!!! anonsagainstanonymous > Nibby
03/19/2014 at 12:27

Kinja'd!!!0

Thank you! I'd completely forgotten about pastebin


Kinja'd!!! anonsagainstanonymous > crowmolly
03/19/2014 at 12:29

Kinja'd!!!0

terrible kinja is terrible kinja. Thank you! I'll try and decipher this.


Kinja'd!!! anonsagainstanonymous > crowmolly
03/19/2014 at 12:33

Kinja'd!!!1

whoah it worked! That was so much simpler than other examples I've seen. Thank you very much!


Kinja'd!!! anonsagainstanonymous > vorspringing
03/19/2014 at 12:37

Kinja'd!!!0

Thank you. I got it working thanks to crowmolly. they showed me a very simple way to do this and I'm eternally grateful.


Kinja'd!!! David Talmage > anonsagainstanonymous
03/19/2014 at 12:41

Kinja'd!!!0

Your problem is that numbers in Javascript are passed by value not by reference. Make the {cost,cash,data}Prompt functions return a value. Assign the value to cashNumber or costNumber or whatever.


Kinja'd!!! crowmolly > anonsagainstanonymous
03/19/2014 at 14:19

Kinja'd!!!0

Wow, thanks for the title change! No worries, it's a question of variable scope. Global vars (as stated by somebody else I think) should be only used as a last resort.


Kinja'd!!! CalzoneGolem > anonsagainstanonymous
03/19/2014 at 14:47

Kinja'd!!!0

That's not a number, cumface. Try again.

Lol. Make sure that comes out before you move into production.


Kinja'd!!! Eric the RC guy > anonsagainstanonymous
03/19/2014 at 16:59

Kinja'd!!!0

Edit: I see that you already got this solved, sorry for the late response. I'll leave this here just as a basic explainer for return functions, they operate the same way in just about every language.

Sorry, I got super busy at work today and am about to leave now but I'll take a look at that code tomorrow morning and get back to you if you haven't got an answer by then.

The return function basically allows you to use a sub-routine to assign a value to a variable. IE (warning: pseudo-code ahead):

[code]
a = doMath(12,15);

function doMath(x as integer, y as integer) {
return x * y;
}
[/code]

That code will make a = 180. Letting functions return values eliminates issues with assigning global variables and worrying about variable scope. In general it is best practice to never use global variables in any language, and the reason I forced myself to learn to always do it that way is because every language handles global variables differently. If you never use them you never have to remember which language handles them in which way.


Kinja'd!!! Eric the RC guy > CalzoneGolem
03/19/2014 at 17:00

Kinja'd!!!1

What he said, I learned this the hard way.